
package com.controller.web;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.controller.dao.page.Page;
import com.controller.entity.User;
import com.controller.services.UserServices;

/**
 * 
 * @author dengjiachang
 * @time 2013-1-17 ע��@RequestMappingΪӳ���ַ����Ϊ�κ����(��Ʊ��벻���ظ�)
 */
@RequestMapping("/user")
@Controller
public class UserController {

	@Resource
	UserServices userServices;

	/**
	 * ����ҳ�� ,û�к�׺��β������ required = false��ʾ����ò�����Բ���ò���,�෴Ϊtrue��ʾ�����иò���
	 * defaultValue = ""��ʾ�ò���Ĭ��ֵ
	 * 
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/index")
	public String index(Model model, HttpSession session, @RequestParam(value = "id", required = false, defaultValue = "") String id) {

		model.addAttribute("user", userServices.findById(id));// ����û�����ID�����û���Ϣ
		return "/user/index";
	}
	@RequestMapping(value = "/page")
	public String page(Model model, HttpSession session, @RequestParam(value = "id", required = false, defaultValue = "") String id) {
		
		return "/user/page";
	}

	/**
	 * ɾ���û� �����rest��������(������������ַ)
	 * 
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}")
	public String edit(Model model, HttpSession session, @PathVariable(value = "id") String id) {
		model.addAttribute("user", userServices.findById(id));// ����û�����ID�����û���Ϣ
		return "/user/edit";
	}

	/**
	 * �����û�,������.do��׺��β������
	 * 
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/addUser.do")
	public String addUser(Model model, HttpSession session, User user) {

		userServices.addUser(user);// �����û�
		// ��ת
		return "redirect:/user/index?id=" + user.getId();
	}

	/**
	 * ɾ���û� �����rest��������(������������ַ)
	 * 
	 * @param model
	 * @param id �û�����
	 * @param session
	 * @return
	 * @throws IOException
	 */
	// ��ʾ����һ��json�ַ�
	@RequestMapping(value = "/delUser/{id}")
	@ResponseBody
	public void delUser(Model model, HttpSession session, @PathVariable(value = "id") Integer id, HttpServletResponse response) throws IOException {

		try {
			userServices.delUser(id);// ɾ���û�
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print("ɾ��ɹ�");
		} catch (IOException e) {
			response.getWriter().print("ɾ��ʧ��");
		}
	}

	/**
	 * �����û�
	 * 
	 * @param model
	 * @param user �û�����
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/updateUser")
	public String updateUser(Model model, HttpSession session, User user) {

		userServices.updateUser(user);// �����û���Ϣ
		// ��ת
		return "redirect:/user/edit/" + user.getId();
	}

	/**
	 * �����û�
	 * 
	 * @param model
	 * @param session
	 * @param start ��ǰ�ڼ�ҳ
	 * @param limit ÿҳ��ʾ����
	 * @return
	 */
	@RequestMapping(value = "/selUser")
	public String selUser(Model model, HttpSession session, User user, @RequestParam(value = "start", required = false, defaultValue = "1") Integer start,
			@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {

		Page page = userServices.getUserList(user, start, limit);
		model.addAttribute("page", page);// ��ҳ�����û�
		model.addAttribute("user", user);
		return "/user/manager";
	}
}
